home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Aminet 31
/
Aminet 31 (1999)(Schatztruhe)[!][Jun 1999].iso
/
Aminet
/
dev
/
c
/
vbccm68k.lha
/
vbcc
/
doc
/
interface.doc
< prev
next >
Wrap
Text File
|
1999-03-07
|
29KB
|
810 lines
(c) in 1995-99 by Volker Barthelmann
This document is under construction!
This document describes some of the internals of vbcc and tries to explain
what has to be done to write a code generator for vbcc.
However if someone wants to write one, I suggest to contact me first,
so that it can be integrated into the source tree.
You have to create a new directory for the new target named
machines/<target-name> and write the files machine.c, machine.h
and machine.dt. The compiler for this target will be called
vbcc<target-name> and can be built by the statement
"make TARGET=<target-name> bin/vbcc<target-name>".
From now on integer means any of {char, short, int, long} or their
unsigned couterparts. Arithmetic means integer or float or double.
Elementary type means arithmetic or pointer.
If you intend to write a code generator for a machine with multiple
different kinds of pointers you might have some problems.
THE INTERMEDIATE CODE
vbcc will generate intermediate code for every function and pass this code
to the code generator which has to convert it into the desired output.
In the future there may be a code generator generator which reads a machine
description file and generates a code generator from that, but it is not
clear whether this could simplify much without taking penalties in the
generated code.
Anyway this would be a layer on top of the current interface to the code
generator so that the interface described in this document would still be
valid and accessable.
The intermediate code is represented as a doubly linked list of quadruples
(I am calling them ICs from now on) consisting mainly of an operator, two
source operands and a target. They are represented like this:
struct IC{
struct IC *prev;
struct IC *next;
int code;
int typf;
[...]
struct obj q1;
struct obj q2;
struct obj z;
[...]
};
The only members relevant to the code generator are 'prev', 'next', 'code',
'typf', 'q1', 'q2' and 'z'.
'prev' and 'next' are pointers to the previous and next IC.
The first IC has 'prev'==0 and the last one has 'next'==0.
'typf' is the type of the operands of this IC. This can be one of:
#define CHAR 1
#define SHORT 2
#define INT 3
#define LONG 4
#define FLOAT 5
#define DOUBLE 6
#define VOID 7
#define POINTER 8
#define ARRAY 9
#define STRUCT 10
#define UNION 11
#define ENUM 12 /* not relevant for code generator */
#define FUNKT 13
and can be additionally or'ed by
#define UNSIGNED 16
#define CONST 64
#define VOLATILE 128
#define UNCOMPLETE 256
However only UNSIGNED is of real importance for the code generator.
'typf'&NQ yields the type without any qualifiers, 'typf'&NU yields
the type without any qualifiers but UNSIGNED.
'q1', 'q2' and 'z' are the source1 (quelle1 in German), source2 and target
(ziel).
If a result has to be computed, it always will be stored in the object 'z'
and the objects 'q1' and 'q2' usually may not be destroyed during this
operation.
The objects are described by this structure.
struct obj{
int flags;
int reg;
struct Var *v;
struct AddressingMode *am;
union atyps{
zchar vchar;
zchar vuchar;
zshort vshort;
zushort vushort;
zint vint;
zuint vuint;
zlong vlong;
zulong vulong;
zfloat vfloat;
zdouble vdouble;
zpointer vpointer;
}val;
};
'flags' describes what kind the object is. It can be a combination of
#define VAR 1
The object is a variable. The pointer to its struct Var is in 'v'.
'val.vlong' vontains an offset that has to be added to it.
A struct Var looks like:
struct Var{
int storage_class;
[...]
char *identifier;
[...]
zlong offset;
[...]
};
The relevant entries are:
'identifier':
The name of the variable. Usually only of interest for variables
with external-linkage.
'storage_class':
One of:
#define AUTO 1
#define REGISTER 2
#define STATIC 3
#define EXTERN 4
#define TYPEDEF 5 /* not relevant */
If the variable is not assigned to a register (i.e. bit REG
is not set in the flags of the corresponding struct obj) then
the variable can be addressed in the following ways (with
examples of 68k-code):
'storage_class' == AUTO or 'storage_class' == REGISTER:
'offset' contains the offset inside the local-variables section.
The code generator must decide how it's going to handle the
activation record.
If 'offset' < 0 then the variable is a function argument on the
stack. In this case the offset in the parameter-area is
- ('offset' + 'maxalign').
The code generator may have to calculate the actual offset
to a stack- or frame-pointer from the value in 'offset'.
'offset'+'val.vlong'(sp)
Note that 'storage_class' REGISTER is equivalent to AUTO - whether
the variable is actually assigned a register is specified by
the bit REG in the 'flags' of the 'struct obj'.
'storage_class' == EXTERN
The variable can be addressed through its name in 'identifier'.
'val.vlong'+_'identifier'
'storage_class' == STATIC
The variable can be addressed through a numbered label. The
label number is stored in 'offset'.
'val.vlong'+l'offset'
#define KONST 2
The object is a constant. Its value is in the corresponding (to 'typf')
member of 'val'.
#define DREFOBJ 32
The content of the location in memory the object points to is used.
#define REG 64
The object is a register. 'reg' contains its number.
#define VARADR 128
The address of the object is to be used. Only together with static
variables (i.e. 'storage_class' STATIC or EXTERN).
The possible combinations of these flags should be:
0 (no object)
KONST
REG
VAR
VAR|REG
REG|DREFOBJ
VAR|DREFOBJ
VAR|REG|DREFOBJ
VAR|VARADR
Also some other bits which are not relevant to the code generator may be set.
Constants will usually be in 'q2' if possible. One of the sources always is
not constant and the target is always an lvalue.
Unless otherwise specified all operands of an IC are of the type 'typf'
(which may be further restricted by 'code'). However not all objects have
to be used.
This depends on 'code' and is listed below. In most cases (i.e. when not
explicitly stated) 'typf' is an elementary type (i.e. arithmetic or pointer).
'am' can be used to store information on special addressing modes.
This has to be handled by the by the code generator. However 'am' has to be 0
or has to point to a struct AddressingMode that was allocated using malloc()
when the code generator returns.
'val' stores either the value of the object if it is a constant or an offset
if it is a variable.
'code' describes the operation and can be one of:
#define ASSIGN 2
Copy 'q1' to 'z'. 'q2.val.vlong' contains the size of the objects (this is
necessary if it is an array or a struct). 'typf' does not have to be an
elementary type!
The only case where 'typf' == ARRAY should be in automatic initializations.
It is also possible that ('typf'&NQ) == CHAR but the size is != 1. This is
created for an inline memcpy/strcpy where the type is not known.
#define OR 16
#define XOR 17
#define AND 18
Bitwise boolean operations. q1,q2->z.
All operands are integers.
#define LSHIFT 25
#define RSHIFT 26
Bit shifting. q1,q2->z. 'q2' is the number of shifts.
All operands are integers.
#define ADD 27
#define SUB 28
#define MULT 29
#define DIV 30
Standard arithmetic operations. q1,q2->z.
All operands are of arithmetic types (integers or floating point).
#define MOD 31
Modulo (%). q1,q2->z.
All operands are integers.
#define KOMPLEMENT 33
Bitwise complement. q1->z.
All o